﻿<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page import="my.myorm.tool.jdbc.SQLExecutor" %>
<%@ page import="my.myorm.tool.jdbc.SQLManager" %>
<%@ page import="java.util.*" %>
<%@ page import="my.myorm.tool.jdbc.ResultList" %>
<%@ page import="com.util.SuperString" %>
<%@ page import="com.util.SuperPage" %>
<%@ page import="com.mtbu.comm.Util" %>
<%@ page import="com.mtbu.dao.DAOManager" %>
<%@ page import="com.mtbu.service.LogUtil" %>
<%@ page import="com.mtbu.bean.*"%>
<%@ page import="com.util.SuperDate"%>
<%@ page import="java.io.File"%>
<%@ page import="my.tool.ConfigProp" %>
<%@ page import="com.mtbu.comm.*"%>
<%@ page import="my.mypool.core.memcache.*"%>

<%@ include file="sys_commcheck.jsp" %>



<%
	//System.out.println("request:" + request.getRequestURI());
	//System.out.println("request:" + request.getRequest());

    String act = SuperString.notNullTrim(request.getParameter("act"));
	boolean isBatchdel = act.equalsIgnoreCase("batchdel");
	//System.out.println("isBatchdel:" + isBatchdel);
    if(!act.equalsIgnoreCase("mod") && !act.equalsIgnoreCase("del")) act = "add";
    boolean isAdd = act.equalsIgnoreCase("add");
    boolean isMod = act.equalsIgnoreCase("mod");
    boolean isDel = act.equalsIgnoreCase("del");



    String curFile = SuperPage.getCurURLFile(request);
    String backFile = SuperString.replace(curFile, "moddo", "");
    String curName = "STEP";

    int id = SuperString.getInt(request.getParameter("id"));
	int pid = SuperString.getInt(request.getParameter("pid"));
	int type = SuperString.getInt(request.getParameter("type"));
	String name = SuperString.notNullTrim(request.getParameter("name"));
	String beanname = SuperString.notNullTrim(request.getParameter("beanname"));
	int prestepid = SuperString.getInt(request.getParameter("prestepid"),0);
	int nextstepid = SuperString.getInt(request.getParameter("nextstepid"),0);


    if(!isBatchdel && !isAdd && id <= 0) {
        Util.callInfo("错误的调用方法！", request, response);
        return;
    }
    SuperDate sdnow = new SuperDate();
	Step record = null;

    if(isDel) {

		record = DAOManager.stepDao.findStep(id);

		if(record == null){
			Util.callInfo("删除失败，没有找到该id的记录！",request, response);
			return;
		}

		try{
			DAOManager.stepDao.delete(record);
		}
		catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){
			Util.callInfo("删除失败，破坏参照完整性！", request, response);
			return;
		}
    }

	if(isAdd) {

		record = new Step();

		try{
			record.setProjectId(pid);
			record.setType(type);
			record.setName(name);
			record.setBeanName(beanname);
			record.setPreStepId(prestepid);
			record.setNextStepId(nextstepid);
			record.setStatus(0);

			record.setUpdateTime(sdnow.getDate());
			record.setCreateTime(sdnow.getDate());


			DAOManager.stepDao.save(record);

			Step stepPre = record.getPreStep();
			if( stepPre!=null ){
				stepPre.setNextStepId(record.getId());
				DAOManager.stepDao.update(stepPre);
			}


			Step stepNext = record.getNextStep();
			if( stepNext!=null ){
				stepNext.setPreStepId(record.getId());
				DAOManager.stepDao.update(stepNext);
			}
		}
		catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){
			Util.callInfo("增加失败:" + e.getMessage(), request, response);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			Util.callInfo("增加失败:" + e.getMessage(), request, response);
			return;
		}
	}


	if(isMod) {

		record = DAOManager.stepDao.findStep(id);

		if(record == null){
			Util.callInfo("修改失败，没有找到该id的记录！",request, response);
			return;
		}

		try{
			record.setType(type);
			record.setName(name);
			record.setBeanName(beanname);
			record.setPreStepId(prestepid);
			record.setNextStepId(nextstepid);

			record.setUpdateTime(sdnow.getDate());

			DAOManager.stepDao.update(record);


			Step stepPre = record.getPreStep();
			if( stepPre!=null ){
				stepPre.setNextStepId(record.getId());
				DAOManager.stepDao.update(stepPre);
			}

			Step stepNext = record.getNextStep();
			if( stepNext!=null ){
				stepNext.setPreStepId(record.getId());
				DAOManager.stepDao.update(stepNext);
			}
		}
		catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){
			e.printStackTrace();
			Util.callInfo("修改失败:" + e.getMessage(), request, response);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			Util.callInfo("修改失败:" + e.getMessage(), request, response);
			return;
		}
    }

/*
	String serverIp = PropUtil.getInstance("memcache").getProperty("my.memcache.server.host", "");
	int serverPort = Integer.parseInt( PropUtil.getInstance("memcache").getProperty("my.memcache.server.port") ,10);
	int idle = Integer.parseInt( PropUtil.getInstance("memcache").getProperty("my.memcache.server.maxIdle") ,10);
	int initIdleCapacity = Integer.parseInt( PropUtil.getInstance("memcache").getProperty("my.memcache.server.initIdleCapacity") ,10);
	int expire = Integer.parseInt( PropUtil.getInstance("memcache").getProperty("my.memcache.server.expire") ,10);

	MemCacheClient mcc = new MemCacheClient(serverIp, serverPort, idle, initIdleCapacity);

	System.out.println( "mcc.flush:" + mcc.flush() );
*/
	LogUtil.writeLog(Constant_UserName, Util.getActText(act) + curName,
			id + "|" + record.toLogString(), request, null);


    response.sendRedirect(backFile + "?pid="+record.getProjectId()+  SuperPage.getQueryAnd(request, "act,id"));
    return;
%>